package io.github.yotta.logger.manager.manager;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.yotta.common.api.model.Paging;
import io.github.yotta.logger.api.request.LogPageQueryRequest;
import io.github.yotta.logger.manager.entity.LogOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yotta
 * @date 2023/3/26 18:34
 */
@Component
public class LogManagerImpl extends ServiceImpl<BaseMapper<LogOperation>, LogOperation> implements LogManager {

    @Override
    public Paging<LogOperation> page(LogPageQueryRequest request) {
        final Page<LogOperation> page = this.lambdaQuery()
                .eq(StringUtils.isNotEmpty(request.getBizId()), LogOperation::getBizId, request.getBizId())
                .eq(StringUtils.isNotEmpty(request.getApplication()), LogOperation::getApplication, request.getApplication())
                .eq(StringUtils.isNotEmpty(request.getModel()), LogOperation::getModel, request.getModel())
                .eq(StringUtils.isNotEmpty(request.getName()), LogOperation::getName, request.getName())
                .ge(Objects.nonNull(request.getStart()), LogOperation::getOccurTime, request.getStart())
                .le(Objects.nonNull(request.getEnd()), LogOperation::getOccurTime, request.getEnd())
                .eq(Objects.nonNull(request.getTenantId()), LogOperation::getTenantId, request.getTenantId())
                .page(new Page<>(request.getPageNo(), request.getPageSize()));
        return convert(page, this::convert);
    }

    @Override
    public List<LogOperation> list(LogPageQueryRequest request) {
        return this.lambdaQuery()
                .eq(StringUtils.isNotEmpty(request.getBizId()), LogOperation::getBizId, request.getBizId())
                .eq(StringUtils.isNotEmpty(request.getApplication()), LogOperation::getApplication, request.getApplication())
                .eq(StringUtils.isNotEmpty(request.getModel()), LogOperation::getModel, request.getModel())
                .eq(StringUtils.isNotEmpty(request.getName()), LogOperation::getName, request.getName())
                .ge(Objects.nonNull(request.getStart()), LogOperation::getOccurTime, request.getStart())
                .le(Objects.nonNull(request.getEnd()), LogOperation::getOccurTime, request.getEnd())
                .eq(Objects.nonNull(request.getTenantId()), LogOperation::getTenantId, request.getTenantId())
                .list();
    }


    private LogOperation convert(LogOperation logOperation) {
        return logOperation;
    }

    public static <R, T> Paging<R> convert(Page<T> page, Function<T, R> mapFunc) {
        if (Objects.isNull(page)) {
            return null;
        }
        final List<R> collect = page.getRecords().stream().map(mapFunc).collect(Collectors.toList());
        // Long total, List<T> data, Integer pageNo, Integer pageSize
        return new Paging<R>(page.getTotal(), collect, longCastToInt(page.getCurrent()), longCastToInt(page.getSize()));
    }

    /**
     * TODO：mybatisPlus返回的分页信息，使用的是Long类型
     */
    private static Integer longCastToInt(long current) {
        return Long.valueOf(current).intValue();
    }
}
